package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.MenuDto;
import org.hibernate.query.NativeQuery;
import org.hibernate.transform.Transformers;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * Created by feng on 2017/2/25.
 */
public class MenuRepositoryImpl {

    @PersistenceContext
    private EntityManager em;

    public List<MenuDto > getMenuByIds( String menuIds, String menuIds2, Integer orgType) {
        String sql = "select MenuId,MenuName,MenuIcon,MenuUrl,MenuFather,MenuType,MenuSort from tb_menu " +
                "where MenuId in (" + menuIds + ") ";
        if(orgType == 0) {
            sql += "and MenuId in (" + menuIds2 + ")";
        }
        Query query = em.createNativeQuery(sql);
        query.unwrap(NativeQuery.class).setResultTransformer( Transformers.aliasToBean(MenuDto.class));
        List<MenuDto> menuDtos = query.getResultList();
        return menuDtos;
    }

    public List<MenuDto> getPowerByUserId(Integer userId) {
        String sql = "select MenuId, SUM(PowerInsert) as PowerInsert, SUM(PowerDelete) as PowerDelete, " +
                "SUM(PowerUpdate) as PowerUpdate,SUM(PowerSelect) as PowerSelect, SUM(PowerCheck) as PowerCheck " +
                "from v_user_actor_menu where UserId=" + userId + " GROUP BY MenuId";
        Query query = em.createNativeQuery(sql);
        query.unwrap(NativeQuery.class).setResultTransformer(Transformers.aliasToBean(MenuDto.class));
        List<MenuDto> menuDtos = query.getResultList();
        return menuDtos;
    }
}
